home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / sharewar / FFE / GRAPH.SWG / 0067_Lightwave C source.pas < prev    next >
Pascal/Delphi Source File  |  1997-05-12  |  3KB  |  113 lines

  1. #define PI            3.1415916
  2. #define TWOPI        6.2831832
  3. #define HALFPI    1.5707958
  4.  
  5. typedef enum {TT_PLANAR,TT_CYLINDRICAL,TT_SPHERICAL} TxType;
  6. typedef enum{ TA_X,TA_Y,TA_Z} TxAxis;
  7.  
  8. typedef struct {
  9.     float xTextureCenter;
  10.     float yTextureCenter;
  11.     float zTextureCenter;
  12.     float xTextureSize;
  13.     float yTextureSize;
  14.     float zTextureSize;
  15.     TxAxis textureAxis;
  16.     TxType textureType;
  17. } Texture;
  18.  
  19. float fract(float x)
  20. {
  21.     return((float)(x-floor(x)));
  22. }
  23.  
  24. void xyztoh(float x,float y,float z,float *h)
  25. {
  26.     if (x == 0.0 && z == 0.0)
  27.         *h = 0.0;
  28.     else {
  29.         if (z == 0.0)
  30.             *h = (x < 0.0) ? HALFPI : -HALFPI;
  31.         else if (z < 0.0)
  32.             *h = -atan(x / z) + PI;
  33.         else
  34.             *h = -atan(x / z);
  35.     }
  36. }
  37.  
  38. void xyztohp(float x,float y,float z,float *h,float *p)
  39. {
  40.     if (x == 0.0 && z == 0.0) {
  41.         *h = 0.0;
  42.         if (y != 0.0)
  43.             *p = (y < 0.0) ? -HALFPI : HALFPI;
  44.         else
  45.             *p = 0.0;
  46.     }
  47.     else {
  48.         if (z == 0.0)
  49.             *h = (x < 0.0) ? HALFPI : -HALFPI;
  50.         else if (z < 0.0)
  51.             *h = -atan(x / z) + PI;
  52.         else
  53.             *h = -atan(x / z);
  54.         x = sqrt(x * x + z * z);
  55.         if (x == 0.0)
  56.             *p = (y < 0.0) ? -HALFPI : HALFPI;
  57.         else
  58.             *p = atan(y / x);
  59.     }
  60. }
  61.  
  62. void TextureUV(Texture *tx,float x,float y,float z,float *u, float *v,int widthTiling, int heightTiling)
  63. {
  64.         float t,s,lon,lat;
  65.     x -= tx->xTextureCenter;
  66.     y -= tx->yTextureCenter;
  67.     z -= tx->zTextureCenter;
  68.     if (tx->textureType == TT_PLANAR) {
  69.         s = (tx->textureAxis == TA_X) ? z / tx->zTextureSize + .5 :
  70.           x / tx->xTextureSize + .5;
  71.         t = (tx->textureAxis == TA_Y) ? -z / tx->zTextureSize + .5 :
  72.           -y / tx->yTextureSize + .5;
  73.         *u = fract(s);
  74.         *v = fract(t);
  75.     }
  76.     else if (tx->textureType == TT_CYLINDRICAL) {
  77.         if (tx->textureAxis == TA_X) {
  78.             xyztoh(z,x,-y,&lon);
  79.             t = -x / tx->xTextureSize + .5;
  80.         }
  81.         else if (tx->textureAxis == TA_Y) {
  82.             xyztoh(-x,y,z,&lon);
  83.             t = -y / tx->yTextureSize + .5;
  84.         }
  85.         else {
  86.             xyztoh(-x,z,-y,&lon);
  87.             t = -z / tx->zTextureSize + .5;
  88.         }
  89.         lon = 1.0 - lon / TWOPI;
  90.         if (widthTiling != 1.0)
  91.             lon = fract(lon) * widthTiling;
  92.         *u = fract(lon);
  93.         *v = fract(t);
  94.     }
  95.     else if (tx->textureType == TT_SPHERICAL) {
  96.         if (tx->textureAxis == TA_X)
  97.             xyztohp(z,x,-y,&lon,&lat);
  98.         else if (tx->textureAxis == TA_Y)
  99.             xyztohp(-x,y,z,&lon,&lat);
  100.         else
  101.             xyztohp(-x,z,-y,&lon,&lat);
  102.         lon = 1.0 - lon / TWOPI;
  103.         lat = .5 - lat / PI;
  104.         if (widthTiling != 1.0)
  105.             lon = fract(lon) * widthTiling;
  106.         if (heightTiling != 1.0)
  107.             lat = fract(lat) * heightTiling;
  108.         *u = fract(lon);
  109.         *v = fract(lat);
  110.     }
  111. }
  112.  
  113.